#include "folderlist.h"
#include "ui_folderlist.h"
#include "button.h"
#include "QLabel"

bool sortByName(Button * button1, Button * button2) {
    return button1->text().toLower()<button2->text().toLower();
}

FolderList::FolderList(QWidget *parent, FileSelector *fs) :
    KeyControllableMenu(parent),
    ui(new Ui::FolderList)
{
    WindowManager::addWidget(this);
    QString unSelectedStyleSheet="background: rgba(255, 255, 255, 15); border: 1px solid rgba(255, 255, 255, 0); text-align: left; color: white;";
    QString selectedStyleSheet="background: qlineargradient(spread:pad, x1:0.07, y1:0.153409, x2:1, y2:1, stop:0 rgba(0, 0, 0, 50), stop:1 rgba(255, 255, 255, 70)); color: white; text-align: left; border: 1px solid rgba(255, 255, 255, 200);";
    QString inactiveSelectedStyleSheet="background: qlineargradient(spread:pad, x1:0.07, y1:0.153409, x2:1, y2:1, stop:0 rgba(255, 255, 255, 100), stop:1 rgba(255, 255, 255, 200)); color: white; text-align: left; border: 1px solid rgba(255, 255, 255, 200)";
    setStyleSheets(selectedStyleSheet,unSelectedStyleSheet, inactiveSelectedStyleSheet);
    this->fs = fs;
    QList <Button *> folderNames;    
    QSqlQueryModel * model= Querier::getModel("SELECT * FROM ListenedFolders");
    for(int i = 0; i < model->rowCount(); i++){
        QString folder =  model->record(i).value("path").toString();
        folder = folder.mid(folder.lastIndexOf("/")+1);
        folderNames.append(new Button (QIcon(""),folder,this,new QLabel(model->record(i).value("folderID").toString())));
    }    
    qSort(folderNames.begin(),folderNames.end(),sortByName);
    setLayout(new QVBoxLayout());
    foreach(Button * folder, folderNames){
        layout()->addWidget(folder);
        addRow(folder);
    }
    ui->setupUi(this);
}

void FolderList::reshowComponents(){
    //reloadFolders();
}

void FolderList::reloadFolders(){

    foreach( QObject * object, children() ) {
         Button * button=qobject_cast<Button *>(object);
         if(button) {
             removeButton(button);
             delete button;
         }
    }
    QList <Button *> folderNames;
    QSqlQueryModel * model= Querier::getModel("SELECT * FROM ListenedFolders");
    for(int i = 0; i < model->rowCount(); i++){
        QString folder =  model->record(i).value("path").toString();
        folder = folder.mid(folder.lastIndexOf("/")+1);
        folderNames.append(new Button (QIcon(":/imgs/Folder.png"),folder,this,new QLabel(model->record(i).value("folderID").toString())));
    }
    qSort(folderNames.begin(),folderNames.end(),sortByName);
    setLayout(new QVBoxLayout());
    foreach(Button * folder, folderNames){
        layout()->addWidget(folder);
        layout()->setAlignment(folder,Qt::AlignTop);
        folder->setIconSize(QSize(64,64));
        folder->setMaximumWidth(QApplication::desktop()->screen(0)->width()/2);
        folder->setMinimumWidth(QApplication::desktop()->screen(0)->width()/2);
        folder->setMinimumHeight(70);
        addRow(folder);
    }
    ui->setupUi(this);
}

void FolderList::buttonSelected(){
    QLabel * label = qobject_cast<QLabel *>(getCurrentButton()->getItem());
    qDebug()<<"mf.folderID = '"+ label->text() + "'";
    fs->setFilter("mf.folderID = '"+ label->text() + "'");
    fs->reloadFiles();
    WindowManager::showWidget(fs);
}

FolderList::~FolderList()
{
    delete ui;
}

void FolderList::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
